package com.cgc.shop.action;

import java.util.HashSet;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cgc.shop.model.Cart;
import com.cgc.shop.model.Orders;
import com.cgc.shop.model.Product;
import com.cgc.shop.service.OrdersService;

@Controller("ordersAction")
@Scope("prototype")
public class OrdersAction extends BaseAction<Orders>{
	public String putOrdersInCart() {
		if (sessionMap.get("cart") == null) {
			sessionMap.put("cart", new Cart(new HashSet<Orders>()));
		}
		Cart cart = (Cart) sessionMap.get("cart");
		Product product = productService.get(model.getProduct().getId());
		cart = ordersService.addOrders(product, cart);
		cart.setTotal(cartService.calculateTotal(cart));
		sessionMap.put("cart", cart);
		return "putOrdersInCart";
	}
}
